﻿var adminMenuTabs, adminPanelBarIsInit = true, adminMenuTabsOpenUrl = [];

function decode(str) {
    $('#javascript-diacritics-decode').html('');

    var result = $('#javascript-diacritics-decode').html(str).html();

    $('#javascript-diacritics-decode').html('');

    return result;
}

function getTabIndex(currentTab) {
    return adminMenuTabs.tabGroup.find('li').index(currentTab);
}

function getSelectedTabIndex() {
    return adminMenuTabs.tabGroup.find('li').index(adminMenuTabs.select());
}

/**
* Show the panel bar menu and slide the tabstrip
*/
function openAdminPanelBar() {
    $('#admin-tabs-container').animate({ marginLeft: '302px' }, 350);
    $('#admin-menu').delay(350).animate({ marginLeft: '0px' }, 350);
}

/*
* select a tab by this index 
*/
function selectTabByIndex(index) {
    adminMenuTabs.select(adminMenuTabs.tabGroup.children("li").eq(index));
}

function getTabByIndex(index) {
    return adminMenuTabs.tabGroup.children("li").eq(index);
}

function closeTab(clickedTab) {
    var tabToClose = $(clickedTab);

    var tabIndex = tabToClose.closest('ul').find('li').index(tabToClose.closest('li'));
    var selectedTabIndex = tabToClose.closest('ul').find('li').index(adminMenuTabs.select());

    //Remove in Url array
    adminMenuTabsOpenUrl.splice(tabIndex, 1);

    //Remove in kendo Tabs
    adminMenuTabs.remove(tabToClose.closest("li"));

    if (tabIndex === selectedTabIndex) {
        selectTabByIndex(tabIndex);
    }
}

function addTab(tabToOpen) {

    //Get Url of new tab
    var tabUrl = $(tabToOpen).attr('href');

    var currentTabIndex = $.inArray(tabUrl, adminMenuTabsOpenUrl);

    //Check if tab is not alreay open
    if (currentTabIndex === -1) {

        if (adminPanelBarIsInit) {
            openAdminPanelBar();

            adminMenuTabs.remove(getTabByIndex(0));

            adminPanelBarIsInit = false;
        }

        var tabTitle = $(tabToOpen).text();
        var tabIndex = adminMenuTabsOpenUrl.length;

        //Add new tab
        adminMenuTabs.append({
            text: '<span>' + tabTitle + '<a href="#" class="close-tab-button">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</a></span>',
            contentUrl: tabUrl,
            encoded: false
        });

        //Save url tab
        adminMenuTabsOpenUrl.push(tabUrl);

        //Select the new tab
        adminMenuTabs.select(getTabByIndex(tabIndex));
    }
    else {
        //Re select tab
        selectTabByIndex(currentTabIndex);
    }
}

$(document).ready(function () {
//    $('#admin-menu').css('margin-left', '-1300px');
//    $('#admin-tabs-container').css('margin-left', '0px');

//    //Init Tabs
//    adminMenuTabs = $("#admin-tabs").kendoTabStrip().data("kendoTabStrip");

//    //Close tab on click
//    $('#admin-tabs').on('click', '.close-tab-button', function () {
//        closeTab(this);
//    });

//    //open new tab on click
//    $('.admin-menu-link').on('click', function () {
//        addTab(this);
//        return false;
//    });
});